//
// Created by csp18 on 2025/2/26.
//

#ifndef NETWORK_TOOL_NETADAPTER_H
#define NETWORK_TOOL_NETADAPTER_H

#include <string>
#include <vector>
#include <mutex>

#include "pcap.h"

#define PCAP_SRC_HOST_STRING "rpcap://192.168.16.1:2002"
#define NCID "38E83022-95EB-41EB-AD91-21C5DB341315"
#define USE_FILTER  1

#define MAX_LEN 1024

using namespace std;

class INetListener;

/*
 *  NetAdapter 网络适配器
 *  功能：
 *      1. 接收和发送网络数据包
 *      2. 储存网络硬件信息
 */
class NetAdapter
{

public:
    NetAdapter();

    string NetDevsList();

    bool openDev(const char *name);

    void readDev();

    //int writeDev(const char* buf, int sendLen);

    static void Notify(unsigned char *user, const struct pcap_pkthdr *header, const unsigned char *pkt_data);

    void addListener(INetListener *listener);

    void removeListener(INetListener *listener);

    void run();

private:
    pcap_if_t *device_find(const char *name);


protected:
    pcap_if_t *allDevs; //所有设备信息
    pcap_t *handle;     //打开的网卡设备句柄

    static std::vector<INetListener *> listeners_;
    std::mutex mtx_; // 保证线程安全
};


#endif //NETWORK_TOOL_NETADAPTER_H
